//
//  MyContactServiceTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/3.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyContactServiceTabVC.h"
#import "MyContactServiceCell.h"
#import "RelationModel.h"
#import "ServiceModel.h"
#import "ServiceEvaluationModel.h"

@interface MyContactServiceTabVC ()<MyContactServiceCellDelegate>
@property(nonatomic,assign) NSInteger ids;
@property(nonatomic,strong) NSMutableArray *relationArray;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,assign) NSInteger pageNumber;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,assign) NSInteger uid;
//@property(nonatomic,assign) NSInteger aid;
//@property(nonatomic,strong) NSString *content;
//@property(nonatomic,assign) NSInteger scope;
//@property(nonatomic,strong) NSString *images;
@property(nonatomic,strong) ServiceEvaluationModel *evaluationModel;
@property(nonatomic,strong) NSIndexPath *indexPath;
@property(nonatomic,assign) NSInteger statu;
@property(nonatomic,strong) RelationModel *relationModel;
@end
static NSString *identifier = @"cell";
@implementation MyContactServiceTabVC
-(NSMutableArray *)relationArray
{
    if (!_relationArray) {
        _relationArray = [NSMutableArray array];
    }
    return _relationArray;
}
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
    }
    return _demandArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.relationModel =[[RelationModel alloc]init];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.evaluationModel = [[ServiceEvaluationModel alloc]init];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = footer;
                [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
                footer.stateLabel.font = [UIFont systemFontOfSize:12];
                footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyContactServiceCell" bundle:nil] forCellReuseIdentifier:identifier];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationEvaluatesave:) name:@"associationEvaluatesave" object:nil];
}

-(void)refresh
{
    self.relationArray = nil;
    self.demandArray = nil;
    self.pageSize = 10;
    self.pageNumber = 1;
    self.uid = 1;
    [self reloadInternetRelation];
}
#pragma mark-查询relation
-(void)reloadInternetRelation
{
//    NSString *uids = [NSString stringWithFormat:@"%ld",self.uid];
    NSString *pageNums = [NSString stringWithFormat:@"%ld",self.pageNumber];
    NSString *pageSizes = [NSString stringWithFormat:@"%ld",self.pageSize];
    self.urlString = [NSString stringWithFormat:@"%@/association/relations",kUrl];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"page":pageNums,
                  @"rows":pageSizes,
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XLog(@"MyContactServiceTabVC:%@",responseObject);
        NSArray *date = [responseObject objectForKey:@"date"];
        if (date.count) {
            for (NSDictionary *dict in date) {
                ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
                [self.relationArray addObject:model];
            }[self.tableView reloadData];
            [WSProgressHUD dismiss];
            if (date.count< 10) {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }else {
                [self.tableView.mj_footer endRefreshing];
            }
        }else {
            [WSProgressHUD dismiss];
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];
    
    
}

-(void)reloadMore
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.pageNumber ++;
    [self reloadInternetRelation];
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark-代理:评价
-(void)associationEvaluateSave
{
    self.dict = @{
                  
                  };
    //@"/associationEvaluate/save
    [CustomeManager POSTWithURLIndex:40 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)associationEvaluatesave:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    [WSProgressHUD showImage:[UIImage imageNamed:@""] status:message];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [WSProgressHUD dismiss];
    });
}
#pragma mark-代理:评价后刷新
-(void)reloadInternetRowData:(NSInteger )row
{
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:row];
//    XLog(@"")

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadData];
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.relationArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    MyContactServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.indexPath = indexPath;
    ServiceModel *model = self.relationArray[indexPath.row];
    cell.serviceModel = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 129;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     ServiceModel *model = self.relationArray[indexPath.row];
    if (self.returnValueBlock) {
        self.returnValueBlock(model, 0);
    };
}
#pragma mark-代理:评价
- (void)myContactServiceCell:(MyContactServiceCell *)myContactServiceCell withEvaluateBtn:(UIButton *)evaluateBtn withModel:(ServiceModel *)model withIndexPath:(nonnull NSIndexPath *)indexPath
{
    __weak typeof (self)weakSelf = self;
    if (self.returnValueBlock) {
        weakSelf.returnValueBlock(model, 1);
        
    
    };
}
@end
